/*
 * Copyright 2006 Kevin Wetzels 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0 
 *       
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package be.roam.drest.service.bloglines.xml;

import java.util.Stack;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import be.roam.drest.service.bloglines.BloglinesSubscription;

/**
 * Handler for parsing the Bloglines subscriptions XML.
 */
public class BloglinesSubscriptionsHandler extends DefaultHandler {
    
    private Stack<BloglinesSubscription> entryStack;
    
    public BloglinesSubscriptionsHandler() {
        entryStack = new Stack<BloglinesSubscription>();
    }
    
    public BloglinesSubscription getEntry() {
        if (!entryStack.isEmpty()) {
            return entryStack.peek();
        }
        return null;
    }
    
    /**
     * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if ("outline".equals(qName)) {
            BloglinesSubscription entry = new BloglinesSubscription();
            entry.setId(parseLong(attributes.getValue("BloglinesSubId")));
            entry.setIgnore(parseBoolean(attributes.getValue("BloglinesIgnore")));
            entry.setNrUnreadMessages(parseInt(attributes.getValue("BloglinesUnread")));
            entry.setTitle(attributes.getValue("title"));
            entry.setType(attributes.getValue("type"));
            entry.setUrlHtml(attributes.getValue("htmlUrl"));
            entry.setUrlXml(attributes.getValue("xmlUrl"));
            entryStack.push(entry);           
        }
    }

    /**
     * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("outline".equals(qName)) {
            if (entryStack.size() > 1) {
                BloglinesSubscription currentEntry = entryStack.pop();
                if (!entryStack.isEmpty()) {
                    entryStack.peek().addEntry(currentEntry);
                }
            }
        }
    }
    
    private int parseInt(String intString) {
        return (intString == null ? 0 : Integer.parseInt(intString));
    }
    
    private long parseLong(String longString) {
        return (longString == null ? 0 : Long.parseLong(longString));
    }
    
    private boolean parseBoolean(String booleanString) {
        return (booleanString == null ? false : "1".equals(booleanString));
    }
    

}
